package com.inspur.dgov.policy.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspur.dgov.common.tool.api.R;
import com.inspur.dgov.policy.entity.FileAnalysis;
import com.inspur.dgov.policy.entity.InfoPolicy;
import com.inspur.dgov.policy.service.FileAnalysisService;
import com.inspur.dgov.policy.service.InfoPolicyService;
import com.inspur.dgov.policy.service.PolicyCollectInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 政策统计分析
 *
 * @Author chiyupei
 * @Date 20/6/9 下午4:04
 * @Version 1.0
 * @Description 政策统计分析
 */
@RestController
@RequestMapping("/statistic")
@Api(value = "政策统计分析", tags = "政策统计分析")
public class PolicyStatisticController {

    @Autowired
    private PolicyCollectInfoService policyCollectInfoService;

    @Autowired
    private InfoPolicyService infoPolicyService;

    @Autowired
    private FileAnalysisService fileAnalysisService;

    @ApiOperation(value = "政策数量统计", notes = "", position = 1)
    @GetMapping("/policyCount")
    public R policyCount() {
        JSONObject object = new JSONObject();
        //采集源
        int collectCount = this.policyCollectInfoService.count();
        object.put("collectCount", collectCount);

        List<InfoPolicy> policyList = this.infoPolicyService.list();
        List<FileAnalysis> analysisList = this.fileAnalysisService.list();
        //政策数量=政策+解读
        int policyCount = policyList.size();
        int analysisCount = analysisList.size();
        object.put("policyCount", policyCount + analysisCount);

        //待审核
        int toReviewPolicyCount = 0;
        int toReviewAnalysisCount = 0;
        //已审核
        int reviewedPolicyCount = 0;
        int reviewedAnalysisCount = 0;
        //未发布
        int unpublishedPolicyCount = 0;
        int unpublishedAnalysisCount = 0;
        //已发布
        int publishedPolicyCount = 0;
        int publishedAnalysisCount = 0;
        for (InfoPolicy policy : policyList) {
            String status = policy.getBasicStatus();
            String state = policy.getBasicState();
            if ("1".equals(status))
                toReviewPolicyCount++;
            if ("2".equals(status))
                reviewedPolicyCount++;
            if ("1".equals(state)) {
                publishedPolicyCount++;
            } else {
                unpublishedPolicyCount++;
            }
        }
        for (FileAnalysis analysis : analysisList) {
            String status = analysis.getBasicStatus();
            String state = analysis.getBasicState();
            if ("1".equals(status))
                toReviewAnalysisCount++;
            if ("2".equals(status))
                reviewedAnalysisCount++;
            if ("1".equals(state)) {
                publishedAnalysisCount++;
            } else {
                unpublishedAnalysisCount++;
            }
        }
        object.put("toReviewCount", toReviewPolicyCount + toReviewAnalysisCount);
        //已审核
        object.put("reviewCount", reviewedPolicyCount + reviewedAnalysisCount);
        //未发布
        object.put("unpublishCount", unpublishedPolicyCount + unpublishedAnalysisCount);
        //已发布
        object.put("publishedCount", publishedPolicyCount + publishedAnalysisCount);
        return R.data(object);
    }
}
